package com.codebo.homiematch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebo.homiematch.mapper.TeamMapper;
import com.codebo.homiematch.model.dto.TeamDTO;
import com.codebo.homiematch.model.dto.TeamJoinRequest;
import com.codebo.homiematch.model.dto.TeamListRequest;
import com.codebo.homiematch.model.entity.Team;
import com.codebo.homiematch.model.entity.UserTeam;
import com.codebo.homiematch.model.vo.TeamVO;
import com.codebo.homiematch.service.TeamService;
import com.codebo.homiematch.service.UserTeamService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 25912
 * @description 针对表【team(队伍信息)】的数据库操作Service实现
 * @createDate 2025-07-21 17:39:32
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team>
        implements TeamService
{

    @Resource
    private UserTeamService userTeamService;

    @Cacheable(value = "teamList", key = "#teamListRequest")
    @Override
    public Page<TeamVO> getTeamList(TeamListRequest teamListRequest)
    {

        int status = teamListRequest.getStatus();
        int current = teamListRequest.getCurrent();
        int pageSize = teamListRequest.getPageSize();
        String sortField = teamListRequest.getSortField();
        String sortOrder = teamListRequest.getSortOrder();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", status);
        queryWrapper.orderBy(sortField != null, !sortOrder.equals("descend"), sortField);

        Page<Team> postPage = this.page(new Page<>(current, pageSize), queryWrapper);

        Page<TeamVO> teamVOPage = new Page<>(current, pageSize, postPage.getTotal());

        List<TeamVO> teamVOList = postPage.getRecords().
                stream()
                .map(this::getTeamVO)
                .collect(Collectors.toList());
        QueryWrapper queryWrapper1 = new QueryWrapper();
        teamVOList.forEach(teamVO -> teamVO.setInJoin(
                isJoin(teamVO.getId(), teamListRequest.getUserId())
        ));
        teamVOPage.setRecords(teamVOList);

        return teamVOPage;
    }

    @Override
    public TeamVO getTeamVO(Team team)
    {
        TeamVO teamVO = new TeamVO();
        BeanUtil.copyProperties(team, teamVO);
        return teamVO;
    }

    @Override
    public void create(TeamDTO teamDTO)
    {
        Team team = new Team();
        BeanUtil.copyProperties(teamDTO, team);
        save(team);
        UserTeam userTeam = new UserTeam();
        userTeam.setUserId(teamDTO.getUserId());
        userTeam.setTeamId(team.getId());
        userTeamService.save(userTeam);
    }

    @Override
    public boolean joinTeam(TeamJoinRequest teamJoinRequest)
    {
        UserTeam userTeam = new UserTeam();
        BeanUtil.copyProperties(teamJoinRequest, userTeam);
        return userTeamService.save(userTeam);
    }

    @Override
    public boolean isJoin(long teamId, long userId)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("team_id", teamId);
        queryWrapper.eq("user_id", userId);
        return userTeamService.count(queryWrapper) > 0;

    }


}




